# -*- encoding: utf-8 -*-
##############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see http://www.gnu.org/licenses/.
#
##############################################################################

from openerp import models, fields, api
import sequenceUtil

# class one_dimensional_rule(models.Model):
#     """
#     一维排列规则
#     """
#     _name = 'one.dimensional.rule'

#     name = fields.Char(u'说明')
#     rule = fields.Char(string = u'排列规则', default=':',help='<100,<200,~500,>200,>100  :')

#     @api.model
#     def compute_array(self,length,num=None):
#         return sequenceUtil.cal_spacing(self.rule,length,num)

class reinforcement_scheme(models.Model):
    """
    二维布局规则
    """

    _name = 'two.dimensional.rule' 

    name = fields.Char(u'说明')

    scheme_id = fields.Many2one('reinforcement.scheme',string="配筋方案",required=True)
    # horizontal_rule = fields.Many2one(comodel_name='one.dimensional.rule',
    #                                string=u'横排规则')

    # horizontal_diameter = fields.Float(u'横排钢筋直径Φ')

    horizontal_qty = fields.Integer(u'横排钢筋数量')
    # vertical_qty = fields.Integer(u'竖排钢筋数量')

    diameter = fields.Many2one('product.attribute.value', string=u'钢筋直径Φ',domain="[('attribute_id.name','ilike',u'直径')]")
    # vertical_diameter = fields.Float(u'竖排排钢筋直径Φ')

    # vertical_rule = fields.Many2one(comodel_name='one.dimensional.rule',
    #                                string=u'竖排规则')

    @api.one
    def compute_array(self,length,width):
        hor =sequenceUtil.cal_spacing(':',width,self.horizontal_qty)
        ver = sequenceUtil.cal_spacing('<100,<200,~500,>200,>100',length)
        hor.update(length=length,diameter=self.diameter)
        ver.update(length=width,diameter=self.diameter)
        return {'horizontal':hor,'vertical':ver}

class reinforcement_scheme(models.Model):
    """
    配筋方案
    """

    _name = 'reinforcement.scheme'
    _inherit = ['mail.thread', 'ir.needaction_mixin']
    _description = u'配筋方案'


    name = fields.Char(u'说明',required=True, readonly=True, states={'draft': [('readonly', False)]}, copy=False,default = lambda self: self.env['ir.sequence'].get('reinforcement.scheme') or '/',)

    product_type_id = fields.Many2one('product.attribute.value', string=u'板材类型',domain="[('attribute_id.name','ilike',u'板材类型')]")


    heigth = fields.Integer(u'板厚')

    from_length = fields.Float(u'长度范围从')

    to_length = fields.Float(u'长度范围到')

    design_load = fields.Float(string=u'荷载(KN/m2)')

    rule_ids = fields.One2many(comodel_name='two.dimensional.rule',
        inverse_name='scheme_id',
        string=u'排列规则',copy=True)

    active = fields.Boolean(u'active',default=True)
    
    state = fields.Selection([
            ('draft', u'草稿'),
            ('cancel', u'取消'),
            ('confirm', u'确认'),
            ('allow', u'通过'),
            ('reject', u'不通过'),
            ('done', u'成功')
        ], string='Status', default='draft', readonly=True, required=True, copy=False,
        help="If apply is created, the status is 'Draft'. If apply is confirmed for the particular dates the status is set to 'Confirmed'. If the apply is allow, the status is set to 'Allow'. If apply is cancelled the status is set to 'Cancelled'.")

    @api.one
    def btn_test_compute_products(self):
        return self.compute_products(500,600)

    @api.one
    def compute_products(self,length,width):
        require = []
        for rule in self.rule_ids:
            require.append(rule.compute_array(length,width))
        return require
